﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenTK.Graphics.OpenGL;
using Glu = OpenTK.Graphics.Glu;
using System.Drawing;

namespace SimpleModeler.Graphics
{
    /// <summary>
    /// Repräsentiert eine Sphäre
    /// </summary>
    [Serializable()]
    public class Sphere : SceneObject
    {
        #region Fields
        private IntPtr _spherePtr;
        #endregion // Fields


        #region Properties
        /// <summary>
        /// Anzahl der Scheiben
        /// </summary>
        public int Slices { get; set; }
        /// <summary>
        /// Anzahl der Stapeln
        /// </summary>
        public int Stacks { get; set; }
        /// <summary>
        /// Radius des Sphäre
        /// </summary>
        public double Radius { get; set; }
        #endregion // Properties


        #region Constructors
        /// <summary>
        /// Konstruktor f+r das Klonen
        /// </summary>
        /// <param name="id">ID des neuen Objektes</param>
        public Sphere(ID id) : base(id)
        {
            this._spherePtr = Glu.NewQuadric();
            this.Radius = 1.0f;
            this.Slices = 50;
            this.Stacks = 50;
        }

        /// <summary>
        /// Konstruktor
        /// </summary>
        public Sphere() : base(Color.Azure, new OpenTK.Vector3(1,1,1), new OpenTK.Vector3(0,0,0))
        {
            this._spherePtr = Glu.NewQuadric();
            this.Radius = 1.0f;
            this.Slices = 50;
            this.Stacks = 50;
        }
        #endregion // Constructors


        #region Methods
        /// <summary>
        /// Rendern der einzelnen Vertices für das Objekt
        /// </summary>
        protected override void RenderVertices()
        {
            Glu.QuadricDrawStyle(_spherePtr, OpenTK.Graphics.QuadricDrawStyle.Fill);
            Glu.QuadricNormal(_spherePtr, OpenTK.Graphics.QuadricNormal.Smooth);
            Glu.QuadricTexture(_spherePtr, this.Texture != null);

            if (this.Texture != null)
            {
                if (!Texture.Loaded)
                    Texture.LoadTexture();
                GL.BindTexture(TextureTarget.Texture2D, Texture.TextureID);
            }
            Glu.Sphere(_spherePtr, Radius, Slices, Stacks);
        }

        /// <summary>
        /// Erstellt eine Kopie des Objektes
        /// </summary>
        /// <returns>Kopie des Objektes</returns>
        public override object Clone()
        {
            Sphere c = new Sphere(this.ID);
            SceneObject so = c as SceneObject;
            base.CloneParameters(ref so);
            return c;
        }

        /// <summary>
        /// Gibt Ressourcen frei
        /// </summary>
        public override void Dispose()
        {
            Glu.DeleteQuadric(this._spherePtr);
            base.Dispose();
        }
        #endregion // Methods
    }
}
